package com.itany.corejava.day16;

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerA {

    public static void main(String[] args) {
        System.out.println("服务器");

        try(ServerSocket ss=new ServerSocket(8888)){

            int i=1;

            while(true){
                System.out.println("111111111111111111111111");
                //等待客户端连接,使当前的线程进入阻塞状态,当有客户端连接时回到就绪状态
                try(Socket s=ss.accept();
                    PrintWriter pw=new PrintWriter(
                            s.getOutputStream()
                    )) {
                    System.out.println("222222222222222222222222");

                    InetAddress clientAddr=s.getInetAddress();

                    System.out.println(clientAddr);

                    pw.println("你是第"+(i++)+"个访问者，你的ip是"+clientAddr);

                    pw.flush();


                }catch (Exception e){
                    throw e;
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }




    }
}
